﻿using System;
using System.IO;
using System.Web;

namespace Web.live
{
    /// <summary>
    /// ck 的摘要说明
    /// </summary>
    public class ck : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string qq = string.IsNullOrEmpty(context.Request.Form["qq"]) ? context.Request.QueryString["qq"] : context.Request.Form["qq"];
            string bb = "QQ9021616";
            string pt = context.Request.Form["pt"];
            string path = System.Web.HttpContext.Current.Server.MapPath("/live/login/" + pt + "/");
            string zbj = context.Request.Form["zbj"];
            string dnid = context.Request.Form["dnid"];
            string ckpath = path + dnid + ".html";
            //旧版登录
            if (string.IsNullOrEmpty(dnid))
            {
                path = System.Web.HttpContext.Current.Server.MapPath("/live/ks/login/");
                dnid = context.Request.Form["sn"];
                string filename = path + dnid + ".html";
                StreamWriter sr = null;
                if (File.Exists(System.Web.HttpContext.Current.Server.MapPath("/live/") + dnid + ".html"))
                {
                    try
                    {
                        sr = File.CreateText(filename);
                        string login = context.Request.Form["kslogin"].Replace("/r/n", "");
                        sr.WriteLine(login);
                        //发到远程服务器
                        if (qq != "9021616")
                        {
                            YCCKJB("http://zb.9021616.cn", pt, zbj, dnid, login);
                            YCCKJB("http://110.40.159.217:8888", pt, zbj, dnid, login);
                            YCCKJB("http://zb.520299.com", pt, zbj, dnid, login);
                            YCCKJB("http://live.9021616.cn", pt, zbj, dnid, login);
                        }
                    }
                    catch (Exception err)
                    {
                        bb = err.Message;
                    }
                    finally
                    {
                        if (sr != null)
                            sr.Close();
                    }
                    bb = context.Request.Form["kslogin"];
                }
                else
                {
                    try
                    {
                        sr = File.CreateText(System.Web.HttpContext.Current.Server.MapPath("/TB/login/") + dnid + ".html");
                        sr.WriteLine(context.Request.Form["kslogin"].Replace("/r/n", ""));
                    }
                    catch (Exception err)
                    {
                        bb = err.Message;
                    }
                    finally
                    {
                        if (sr != null)
                            sr.Close();
                    }
                    bb = "没有计票器信息=" + dnid;
                }
            }
            else
            {
                //新版登录
                StreamWriter sr = null;
                if (File.Exists(System.Web.HttpContext.Current.Server.MapPath("/live/" + pt + "/") + dnid + ".html"))
                {
                    string login = context.Request.Form["ck"].Replace("/r/n", "");
                    try
                    {
                        sr = File.CreateText(ckpath);
                        sr.WriteLine(login);
                    }
                    catch (Exception err)
                    {
                        bb = err.Message;
                    }
                    finally
                    {
                        if (sr != null)
                            sr.Close();
                    }
                    //发到远程服务器
                    if (qq != "9021616")
                    {
                        YCCK("http://zb.9021616.cn", pt, zbj, dnid, login);
                        YCCK("http://110.40.159.217:8888", pt, zbj, dnid, login);
                        YCCK("http://zb.520299.com", pt, zbj, dnid, login);
                        YCCK("http://live.9021616.cn", pt, zbj, dnid, login);
                    }
                }
                else
                {
                    try
                    {
                        sr = File.CreateText(System.Web.HttpContext.Current.Server.MapPath("/live/nodnid/") + dnid + ".html");
                        sr.WriteLine(context.Request.Form["ck"].Replace("/r/n", ""));
                    }
                    catch (Exception err)
                    {
                        bb = err.Message;
                    }
                    finally
                    {
                        if (sr != null)
                            sr.Close();
                    }
                    bb = "没有计票器信息=" + dnid + ",直播间=" + zbj;
                }
            }
            context.Response.Write(bb);
        }

        /// <summary>
        /// 远程
        /// </summary>
        private void YCCK(string url, string pt, string zbj, string dnid, string login)
        {
            try
            {
                string thisurl = HttpContext.Current.Request.Url.AbsoluteUri;
                if (!thisurl.Contains(url))
                {
                    System.Net.WebClient WebClientObj = new System.Net.WebClient();
                    System.Collections.Specialized.NameValueCollection PostVars = new System.Collections.Specialized.NameValueCollection();
                    PostVars.Add("pt", pt);
                    PostVars.Add("zbj", zbj);
                    PostVars.Add("dnid", dnid);
                    PostVars.Add("liveid", "");
                    PostVars.Add("ck", login);

                    WebClientObj.UploadValues(url + "/live/ck.ashx?qq=9021616", "POST", PostVars);
                    //string sRemoteInfo = Encoding.UTF8.GetString(byRemoteInfo);
                }
            }
            catch { }
        }

        private void YCCKJB(string url, string pt, string zbj, string dnid, string login)
        {
            try
            {
                string thisurl = HttpContext.Current.Request.Url.AbsoluteUri;
                if (!thisurl.Contains(url))
                {
                    System.Net.WebClient WebClientObj = new System.Net.WebClient();
                    System.Collections.Specialized.NameValueCollection PostVars = new System.Collections.Specialized.NameValueCollection();
                    PostVars.Add("zbj", zbj);
                    PostVars.Add("sn", dnid);
                    PostVars.Add("login", "");
                    PostVars.Add("liveid", "");
                    PostVars.Add("kslogin", login);

                    WebClientObj.UploadValues(url + "/live/ck.ashx?qq=9021616", "POST", PostVars);
                    //string sRemoteInfo = Encoding.UTF8.GetString(byRemoteInfo);
                }
            }
            catch { }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}